package com.ysstech.valuation.thread;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

import com.ysstech.common.enums.YssEnum;
import com.ysstech.common.service.HolidayService;
import com.ysstech.common.util.DateUtil;
import com.ysstech.valuation.entity.Valuation;
import com.ysstech.valuation.service.ValuationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Slf4j
@Component
public class ValuationMainGenerator {
    @Autowired
    private ValuationService valuationService;
    @Autowired
    private HolidayService holidayService;

    /**
     * 生成估值表总入口
     *
     * @param valuation
     * @param list
     * @return
     * @throws Exception
     */

    public void renewValuation(Valuation valuation, List<String> list) throws Exception {
        //  ThreadPool.getThreadPool().execute(new VoucherMain(valuations));
        SimpleDateFormat sdf = new SimpleDateFormat(YssEnum.YSS_DEFAULT_DATE_TIME.getCode());
        Date startDate = sdf.parse("2021-04-16");   // 起始日期
        Date endDate = sdf.parse("2021-04-16");  // 结束日期
        Boolean falg = true;
        while (falg && startDate.getTime() <= endDate.getTime()) {
            List<Valuation> valuations = new ArrayList<>();
            if (!holidayService.isWorkDay(DateUtil.dateToStr(startDate))) {
                startDate = DateUtil.addDay(startDate, 1);
                continue;
            }
            startDate = DateUtil.addDay(startDate, 1);
            for (String accountId : list) {
                Valuation v = new Valuation();
                v.setAccountId(accountId);
                v.setSelfdate(valuation.getSelfdate());
                valuations.add(v);
            }
            if (CollectionUtils.isEmpty(valuations)) {
                continue;
            }
            falg = ThreadSubmit(valuations);
        }
    }


    private Boolean ThreadSubmit(List<Valuation> valuations) throws Exception {
        List<Future> futureList = new ArrayList<>();
        Boolean falg = true;
        valuations.forEach(valuation -> {
            ValuationThread ht = new ValuationThread(valuationService, valuation);
            ThreadPool.getThreadPool().submit(ht);
        });
        while (futureList.stream().filter(f -> !f.isDone()).count() > 0) {
            Thread.sleep(1000);
        }
        for (Future<Boolean> future : futureList) {
            if (!future.get()) {
                falg = false;
                break;
            }
        }
        return falg;
    }

    class VoucherMain extends Thread {
        private List<Valuation> valuations;

        VoucherMain(List<Valuation> valuations) {
            this.valuations = valuations;
        }

        @Override
        public void run() {
            valuations.forEach(valuation -> {
                ValuationThread ht = new ValuationThread(valuationService, valuation);
                ThreadPool.getThreadPool().submit(ht);
            });
        }
    }


}
